package cn.itcast.bos.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.constants.Constants;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class OrderAction extends BaseAction<Order> {
	public String sendAreaInfo;
	public String recAreaInfo;

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	@Action(value = "order_add", results = { @Result(name = "success", location = "index.html", type = "redirect"),
			@Result(name = "login", location = "login.html", type = "redirect") })
	public String orderAdd() {
		Area sendArea = new Area();
		String[] sendStr = sendAreaInfo.split("/");
		sendArea.setProvince(sendStr[0]);
		sendArea.setCity(sendStr[1]);
		sendArea.setDistrict(sendStr[2]);

		Area recArea = new Area();
		String[] recStr = recAreaInfo.split("/");
		recArea.setProvince(recStr[0]);
		recArea.setCity(recStr[1]);
		recArea.setDistrict(recStr[2]);

		model.setSendArea(sendArea);
		model.setRecArea(recArea);
		Customer customer = (Customer) ServletActionContext.getRequest().getSession().getAttribute("customer");
		if (customer == null) {// 用户没有登入
			return LOGIN;
		}
		model.setCustomer_id(customer.getId());
		// 调用webservice
		WebClient.create(Constants.BOS_MANAGEMENT_URL + "/bos_management/services/orderService/order")
				.type(MediaType.APPLICATION_JSON).post(model);
		return SUCCESS;
	}

}
